#!/bin/bash
#
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Install postflight script
#
# Note: this script ends up running from the directory that the installer
# was invoked from, e.g.  maybe / from the GUI, or a /Volumes/blah if
# invoked from CLI

set -e
set -x

function die() {
  echo ERROR "$@" >&2
  exit 1
}

export PATH="$PATH:/usr/bin"
PKG="$1"
DEFAULT_PATH="$2"
TARGET_PATH="$3"
RESOURCES="${PKG}/Contents/Resources"
BASETMPDIR="${TARGET_PATH}/tmp/"
OSX=$(sw_vers -productVersion | cut -d. -f1-2)
VERSION="0.9.1"
PACKAGE_DEPS="setuptools python-dateutil>=1.4,<2 pyyaml google_apputils"
PYTHON_VERSION=$(egrep '[0-9].[0-9]' "${PKG}/Contents/Resources/python_version")
PYTHON="python${PYTHON_VERSION}"
INSTALL_DIR=/usr/local/cauliflowervest/
VE_DIR=cv

### set up sane logging
d=$(date '+%Y%m%d%H%M')
LOGFILE="${BASETMPDIR}/postflight.${d}.$$.log"
exec 1> "${LOGFILE}"
exec 2>&1
echo postflight BEGIN

### create temp work directory
TMPDIR=$(mktemp -d ${BASETMPDIR}/postflightXXXXXX)
ORIGPWD="${PWD}"
trap "rm -rf ${TMPDIR}" EXIT

### find & test easy_install

# this can be somewhat difficult because the version of easy_install
# appropriate for our PYTHON_VERSION and setuptools version might be in
# /usr/local/bin or /usr/bin.

EZ="/usr/bin/easy_install"

# the version in /usr/local/bin might be for a newer python with a newer
# dependency on setuptools. try running it to see if it is willing to run.
if [[ -f /usr/local/bin/easy_install ]]; then
  if [[ -x /usr/local/bin/easy_install ]]; then
    ${PYTHON} /usr/local/bin/easy_install --help 2>/dev/null && \
      EZ="/usr/local/bin/easy_install"
  fi
fi

# easy_install might point the user to easy_install-VERSION instead and fail.
# look around for a specific version.
if [[ "$EZ" = "/usr/bin/easy_install" ]]; then
  if [[ -x "/usr/bin/easy_install-${PYTHON_VERSION}" ]]; then
    EZ="/usr/bin/easy_install-${PYTHON_VERSION}"
  fi
fi

[[ -x "${EZ}" ]] || die "Cannot execute easy_install ${EZ}"

### install virtualenv

${PYTHON} ${EZ} -U virtualenv

### find & test newly installed virtualenv

VIRTUALENV=$(PATH=/usr/bin:/usr/local/bin type -p virtualenv)
[[ -x "${VIRTUALENV}" ]] || die "Cannot execute or find virtualenv"

### create install directory and run virtualenv to create INSTALL_DIR/VE_DIR
oldpwd="$PWD"
mkdir -p "${INSTALL_DIR}"
cd "${INSTALL_DIR}"

### create a distutils __init__.py if it does not exist
### otherwise virtualenv fails.
distutils_path=$(${PYTHON} -c \
    'import sys; import distutils; print distutils.__path__[0]')
[[ -d "${distutils_path}" ]] || die "Cannot find distutils"

if [[ ! -f "${distutils_path}/__init__.py" ]]; then
  ${PYTHON} -c \
'import distutils ; \
print "__version__ = \"%s\"" % distutils.__version__ ; \
print "__revision__ = \"%s\"" % distutils.__revision__' > \
${distutils_path}/__init__.tmp$$
  rm -f ${distutils_path}/__init__.py
  mv -f ${distutils_path}/__init__.tmp$$ ${distutils_path}/__init__.py
fi

# Supply our own install_name_tool first in the PATH so machines without
# the SDK can perform this step.
env PATH="${RESOURCES}/vep:${PATH}" ${PYTHON} ${VIRTUALENV} ${VE_DIR}

VE_PYTHON=${INSTALL_DIR}/${VE_DIR}/bin/python
VE_EZ=${INSTALL_DIR}/${VE_DIR}/bin/easy_install

### install supplied eggs

for egg in ${RESOURCES}/*.egg ; do
  if [ -r "${egg}" ]; then
    echo === Installing egg "${egg}"
    ${VE_PYTHON} ${VE_EZ} -N "${egg}"
  fi
done

### install dependencies
# setuptools does not like to upgrade some packages automatically
# when running setup.py below, e.g. python-dateutil from 1.2 to 1.4.
# so, just upgrade them all in advance. :(

for package in ${PACKAGE_DEPS}; do
  ${VE_PYTHON} ${VE_EZ} -U "${package}"
done

### install main source package

tar -zxf "${RESOURCES}/cauliflowervest-${VERSION}.tar.gz" -C "${TMPDIR}"
cd "${TMPDIR}/cauliflowervest-${VERSION}"
mkdir -p src/tests  # fake out google test
${VE_PYTHON} setup.py install 2>&1
cd "${ORIGPWD}"

### END, remove our debug log
exec 1> /dev/null
exec 2>&1
rm -f "${LOGFILE}"
